package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmalMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*
 * 分类管理的service
 * service层的作用：
 *   调用mapper层的方法查询数据库，如果需要将查询到的数据进行逻辑处理就在service层处理
 *   返回结果给Controller层
 * */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    /* @Autowired进行自动依赖注入，对类、成员变量、成员方法及构造方法进行标注，完成bean自动装配的工作*/
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmalMapper setmealMapper;

    /**
     * 查询所有分类
     *
     * @param CategoryPageQueryDTO
     * @return pageResult
     * @info 接收Controller层传递过来的分页参数，Service层再去调用Mapper层的方法去数据库中查询所有分类  Service->Mapper
     */
    @Override
    public PageResult fetchCategoryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> p = (Page<Category>) categoryMapper.selectAll(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 删除分类
     *
     * @param id
     * @return void
     * @info 接收Controller层传递过来的分类id，Service层再去调用Mapper层的方法去数据库中表中根据id删除分类  Service->Mapper
     */
    @Override
    public void deleteCategoryById(Long id) {
        Integer dishCount = dishMapper.selectDishCountByCategoryId(id);
        if (dishCount > 0) throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);

        Integer setmealCount = setmealMapper.selectSetmealCountByCategoryId(id);
        if (setmealCount > 0) throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        categoryMapper.deleteCategoryById(id);
    }

    /**
     * 启用或者禁用分类
     *
     * @param id
     * @return void
     * @info 接收Controller层传递过来的分类id、分类状态status，Service层再去调用Mapper层的方法去数据库中表中根据id，status启用或者禁用分类  Service->Mapper
     */
    public void openOrCloseCategory(Integer status, Long id) {
        Category category = Category.builder().id(id).status(status).updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId()).build();
        categoryMapper.updateCategoryStatus(category);
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return void
     * @info 接收Controller层传递过来的套餐分类或者菜品分类，Service层再去调用Mapper层的方法去数据库中表添加分类  Service->Mapper
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(1);
        categoryMapper.insertCategory(category);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return void
     * @info 接收Controller层传递过来分类，Service层再去调用Mapper层的方法去数据库中表修改分类  Service->Mapper
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategory(category);
    }

    /**
     * 根据分类的类型type查询分类
     *
     * @param type
     * @return Category
     * @info 接收Controller层传递过来分类类型type，Service层再去调用Mapper层的方法去数据库中表修改分类  Service->Mapper
     */
    public List<Category> fetchCategoryByType(Integer type) {
        return categoryMapper.selectCategoryByType(type);
    }
}
